/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sgsmcan.Utils;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;

/**
 *
 * @author user
 */
public class Utilitario {

    public static ResourceBundle RESOURCE_BUNDLE = ResourceBundle
            .getBundle("config");
    public static final Logger LOG = Logger.getLogger("APP");
    public static final String PATTERN_LETRAS = "[^A-Za-zñÑáéíóúüÁÉÍÓÚ \\- ]";
    public static final String PATTERN_ALFA_NUMERICO = "[^A-Za-z0-9]";
    public static final String PATTERN_EMAIL = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    public static final String PATTERN_NUMEROS = ".*[^0-9].*";

    public static boolean esNulo(String txt) {
        if (txt == null || txt.trim().length() == 0) {
            return true;
        }
        return false;
    }

    public static boolean esRangoValido(String texto, int inicio, int fin) {
        if (esNulo(texto)) {
            return false;
        } else {
            if (texto.length() > fin || texto.length() < inicio) {
                return false;
            }
        }
        return true;
    }

    public static boolean esRangoValido(String texto, int fin) {
        return esRangoValido(texto, 0, fin);
    }

    public static boolean esSoloLetras(String texto) {
        Pattern pattern = Pattern.compile(PATTERN_LETRAS);
        Matcher matcher = pattern.matcher(texto);
        return matcher.find() == true ? false : true;
    }

    public static boolean esAlfaNumerico(String texto) {
        Pattern pattern = Pattern.compile(PATTERN_ALFA_NUMERICO);
        Matcher matcher = pattern.matcher(texto);
        return matcher.find() == true ? false : true;
    }

    public static boolean esCorreoValido(String texto) {
        Pattern pattern = Pattern.compile(PATTERN_EMAIL);
        Matcher matcher = pattern.matcher(texto);
        return matcher.find();
    }

    public static boolean esSoloNumero(String texto) {
        Pattern pattern = Pattern.compile(PATTERN_NUMEROS);
        Matcher matcher = pattern.matcher(texto);
        return matcher.find() == true ? false : true;
    }

    public static boolean esFormatoFechaValido(String date, String formatoFecha) {
        SimpleDateFormat formato = new SimpleDateFormat(formatoFecha, Locale.getDefault());
        formato.setLenient(false);
        try {
            formato.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static void enviarMensajeGlobalValido(String mensaje) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, mensaje, mensaje));
    }

    public static void enviarMensajeGlobalError(String mensaje) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, mensaje, mensaje));
    }

    public static void putFlash(String k, Object v) {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put(k, v);
    }

    public static Object getFlash(String k) {
        return FacesContext.getCurrentInstance().getExternalContext().getFlash().get(k);
    }

    public static void TransferFile(String filename, InputStream in, String path_original) throws Exception {
        OutputStream out = new FileOutputStream(new File(path_original + filename));
        resize(in, out, 250, 190);
    }

    private static void resize(InputStream input, OutputStream output, int width, int height) throws Exception {
        BufferedImage src = ImageIO.read(input);
        BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = dest.createGraphics();
        AffineTransform at = AffineTransform.getScaleInstance((double) width / src.getWidth(), (double) height / src.getHeight());
        g.drawRenderedImage(src, at);
        ImageIO.write(dest, "JPG", output);
        output.close();
    }

    public static Timestamp getFechaActual() {
        return new Timestamp(new Date().getTime());
    }

    public static String getCadenaFechaActual(String formato) {
        Timestamp stamp = new Timestamp(new Date().getTime());
        DateFormat dateFormat = new SimpleDateFormat(formato);
        String fecha = dateFormat.format(stamp);
        return fecha;
    }
    
    public static Date disminuirFecha(Date fechaActual , int cantidad){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(fechaActual);
        calendar.add(Calendar.DAY_OF_YEAR, (-cantidad));
        return calendar.getTime();
    }
    
    public static Timestamp disminuirFecha(Timestamp fechaActual , int cantidad){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(fechaActual);
        calendar.add(Calendar.DAY_OF_YEAR, (-cantidad));
        return new Timestamp(calendar.getTime().getTime());
    }
    
    public static String convertirFormatoFecha(Date date , String formato){

        DateFormat dateFormat = new SimpleDateFormat(formato);
        String fecha = dateFormat.format(date);
        return fecha;
    }
    
    public static boolean esFechaMayor(Date min , Date max){
        
        return min.after(max) ? true : false ;
    }

  
}
